package com.syyo.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.ware.domain.entity.WareOrderTaskDetailEntity;
import com.syyo.gulimall.ware.domain.req.WareOrderTaskDetailReq;
import com.syyo.gulimall.ware.mapper.WareOrderTaskDetailMapper;
import com.syyo.gulimall.ware.service.WareOrderTaskDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 库存工作单
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 10:06:54
 */
@Service
public class WareOrderTaskDetailServiceImpl extends ServiceImpl<WareOrderTaskDetailMapper, WareOrderTaskDetailEntity> implements WareOrderTaskDetailService {

    @Autowired
    private WareOrderTaskDetailMapper wareOrderTaskDetailMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(WareOrderTaskDetailReq req) {
        WareOrderTaskDetailEntity entity = new WareOrderTaskDetailEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = wareOrderTaskDetailMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = wareOrderTaskDetailMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(WareOrderTaskDetailReq req) {
        WareOrderTaskDetailEntity entity = new WareOrderTaskDetailEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = wareOrderTaskDetailMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public WareOrderTaskDetailEntity findOne(Integer id) {
        return wareOrderTaskDetailMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<WareOrderTaskDetailEntity> findAll(Integer pageNum, Integer pageSize, WareOrderTaskDetailReq req) {
        Page<WareOrderTaskDetailEntity> teacherPage = new Page<WareOrderTaskDetailEntity>(pageNum, pageSize);
        QueryWrapper<WareOrderTaskDetailEntity> wrapper = new QueryWrapper<>();
        return wareOrderTaskDetailMapper.selectPage(teacherPage, wrapper);
    }

}